package cn.shujuhai.meeting.controller;

import cn.shujuhai.common.aop.SysLog;
import cn.shujuhai.common.domain.PageRequest;
import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.meeting.domain.MeetingRooms;
import cn.shujuhai.meeting.service.MeetingRoomsService;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @Author: NiuYaHu
 * @Date: 2025/4/8 09:40
 * @Description:
 **/
@RestController
@RequestMapping("/meeting/meetRoom")
@RequiredArgsConstructor
public class MeetRoomController {
    final MeetingRoomsService meetingRoomsService;

    /**
     * 查询会议室列表
     *
     * @param roomName    会议室名称
     * @param capacity    可容纳人数
     * @param pageRequest
     * @return 查询的会议室列表
     */
    @SysLog(value = "会议室列表查询", operationType = "查询", module = "会议室管理模块")
    @GetMapping("/list")
    public ResultVo<PageInfo<MeetingRooms>> getMeetingRoomsList
    (@Validated String roomName, String capacity, @RequestParam Long departmentId, @Valid PageRequest pageRequest) {
        return this.meetingRoomsService.getMeetingRoomsPageInfo(roomName, capacity, departmentId, pageRequest.getPageNum(), pageRequest.getPageSize());
    }

    /**
     * 新增会议室
     *
     * @param meetingRooms 新增会议室对象
     * @return 新增会议室对象
     */
    @SysLog(value = "新增会议室", operationType = "新增", module = "会议室模块")
    @PostMapping("/add")
    public ResultVo<?> save(@RequestBody @Validated MeetingRooms meetingRooms) {
        return this.meetingRoomsService.add(meetingRooms);
    }

    /**
     * 会议室编辑
     *
     * @param meetingRooms 编辑会议室对象
     * @return 更新会议室对象
     */
    @SysLog(value = "会议室编辑", operationType = "编辑", module = "会议室模块")
    @PostMapping("/update")
    public ResultVo<MeetingRooms> update(@RequestBody @Validated MeetingRooms meetingRooms) {
        return this.meetingRoomsService.edit(meetingRooms);
    }

    /**
     * 删除会议室
     *
     * @param roomId 会议室Id
     * @return 删除响应结果
     */
    @SysLog(value = "删除会议室", operationType = "删除", module = "会议室模块")
    @PostMapping("/delete/{roomId}")
    public ResultVo<MeetingRooms> delete(@PathVariable Integer roomId) {
        return this.meetingRoomsService.removeId(roomId);
    }

    /**
     * 获取会议室列表
     *
     * @return 会议室列表
     */
    @GetMapping("/getMeetingRoomsList")
    public ResultVo<?> getMeetingRoomsList() {
        return ResultVo.success(this.meetingRoomsService.list());
    }

    /**
     * 会议室图片上传
     *
     * @param file 上传的图片-文件
     * @return 上传成功后的图片路径
     * @throws IOException
     */
    @SysLog(value = "会议室图片上传", operationType = "上传", module = "会议室模块")
    @PostMapping("/uploadImage")
    public ResultVo<?> uploadImage(@RequestParam MultipartFile file) throws IOException {
        return this.meetingRoomsService.uploadImage(file);

    }

    /**
     * 根据Id获取会议室
     *
     * @return 会议室
     */
    @GetMapping("/getMeetingRoomById")
    public ResultVo<?> getMeetingRoomById(@RequestParam Long roomId) {
        return this.meetingRoomsService.getMeetingRoomById(roomId);
    }

    /**
     * @param roomId 会议室Id
     * @param start  开始时间
     * @param end    结束时间
     */
    @PostMapping("/closeTime")
    public void scheduleRoom(Long roomId, LocalDateTime start, LocalDateTime end) {
        this.meetingRoomsService.scheduleRoom(roomId, start, end);
    }

    @GetMapping("/count")
    public ResultVo<?> meetCount(@Valid PageRequest pageRequest) {
        return this.meetingRoomsService.meetCount(pageRequest.getPageNum(), pageRequest.getPageSize());
    }
}
